package com.zzyl.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
public class CustomerReservationController extends BaseController {
    @Autowired
    private CustomerReservationService customerReservationService;

    /**
     * 根据UserId统计预约表中符合时间点的取消状态（status = 2）的预约数据
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult<Integer> getCancelledReservationCount() {
        //userId对应的是用户表（Member）里的Id
        String userId = UserThreadLocal.getSubject();
        Integer count = customerReservationService.getCancelledReservationCount(userId);
        //返回值是一个1-3的count
        return success(count);
    }

    /**
     * 新增预约信息
     * @param reservationDto
     * @return
     */
    @PostMapping
    @ApiOperation("新增预约信息")
    public ResponseResult addReservation(@RequestBody ReservationDto reservationDto){
        customerReservationService.add(reservationDto);
        return success();
    }

    /**
     * 分页查询预约信息
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public ResponseResult<PageResponse<ReservationVo>> selectPage(@ApiParam(value = "预约状态") Integer status,
                                                                  @ApiParam(value = "分页页码")@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                                  @ApiParam(value = "每页显示记录数")@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        PageResponse<ReservationVo> pages = customerReservationService.selectPage(status,pageSize,pageNum);
        return success(pages);

    }

    /**
     * 取消预约
     * 执行更新 把status设置为2
     * @param id
     * @return
     */
    @PutMapping("/{id}/cancel")
    public ResponseResult delReservation(@PathVariable Long id){
        customerReservationService.cancle(id);
        return success();
    }

    /**
     * 统计每个时间段剩余的预约次数
     * 多个时间段 每半小时为一个时段
     * 请求参数是此时的时间点
     * @param time
     * @return
     */
    @GetMapping("/countByTime")
    public ResponseResult<List<TimeCountVo>> countByTime(Long time){
        //把前端请求的Long类型的time转换成LocalDateTime类型
        List<TimeCountVo> list = customerReservationService.countByTime(LocalDateTimeUtil.of(time));

        return success(list);
    }
}